﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc; 
using Admin.Models; 
using System.Text.RegularExpressions;
using Admin.Filters;
using Data.Video;

namespace Admin.Controllers
{
    [Authorize(Roles = "Admin,SubAdmin,Content")]
    [InitializeSimpleMembership]
    public class NewsController : Controller
    {
        private Htmls html = new  Htmls();
        private VideoEntities db = new VideoEntities();  
        //
        // GET: /News/

        public ActionResult Index(int? CateId)
        {
            ViewBag.CateId = html.SelectListCate(CateId, true);
            if (CateId != null) {
                return View(db.News.Where(e=>e.CateId==CateId).OrderByDescending(e=>e.Orderby).ThenBy(e=>e.DateCreate).ToList());
            }
            else
            {
                return View(db.News.OrderByDescending(e => e.Orderby).ThenBy(e => e.DateCreate).ToList());
            }
        }

        //
        // GET: /News/Details/5

        public ActionResult Details(int id = 0)
        {
            News news = db.News.Find(id);
            if (news == null)
            {
                return HttpNotFound();
            }
            return View(news);
        }

        //
        // GET: /News/Create
        
        public ActionResult Create()
        {
            //ViewBag.UserId = html.SelectListCate(null);
            ViewBag.CateId = html.SelectListCate(null, true); 
            return View();
        }

        //
        // POST: /News/Create 
        [HttpPost, ValidateInput(false)]
        public ActionResult Create(News news)
        {
            if (ModelState.IsValid)
            {
                if (news.CateId<=0)
                {
                    ViewBag.msg = "Chưa chọn nhóm tin";
                }
                else
                {
                    if (string.IsNullOrEmpty(news.Title))
                    {
                        ViewBag.msg = "Tiêu đề không được để trống";
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(news.Content))
                        {
                            ViewBag.msg = "Nội dung không được để trống";
                        }
                        else
                        {
                             
                            news.Image += "";
                            news.Content = news.Content;// FormatHtmlIntoBBCode(news.Content);
                            news.DateCreate = DateTime.Now;
                            
                            news.CateName = db.Category.Find(news.CateId).Name;
                           
                            news.UserName = "";
                            //news = fr.AddThread(news);

                            if (news != null)
                            {
                                db.News.Add(news);
                                db.SaveChanges();
                            }
                            return Redirect("/News/Index?CateId=" + news.CateId);
                        }
                    }
                }
            }
            ViewBag.Cateid = html.SelectListCate(null, true);
            return View(news);
        }
        public string FormatHtmlIntoBBCode(string desc)
        {
            desc = Regex.Replace(desc, @"<br(.*?) />", "[br]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<UL[^>]*>", "[ulist]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/UL>", "[/ulist]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<OL[^>]*>", "[olist]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/OL>", "[/olist]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<LI>", "[*]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/li>", "", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<B>", "[b]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/B>>", "[/b]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<STRONG>", "[strong]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/STRONG>", "[/strong]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<u>", "[u]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/u>", "[/u]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<i>", "[i]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/i>", "[/i]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<em>", "[em]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/em>", "[/em]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<sup>", "[sup]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/sup>", "[/sup]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<sub>", "[sub]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/sub>", "[/sub]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<HR[^>]*>", "[hr]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<STRIKE>", "[strike]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/STRIKE>", "[/strike]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<h1>", "[h1]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/h1>", "[/h1]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<h2>", "[h2]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/h2>", "[/h2]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<h3>", "[h3]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/h3>", "[/h3]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<A HREF=", "[url=", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"<\/A>", "[/url]", RegexOptions.IgnoreCase);
            desc = Regex.Replace(desc, @"'>", "']", RegexOptions.IgnoreCase);

            //match on image tags
            var match = Regex.Matches(desc, @"<IMG[\s\S]*?SRC=\'([\s\S]*?)\'[\s\S]*?>", RegexOptions.IgnoreCase);
            if (match.Count > 0)
                desc = Regex.Replace(desc, match[0].ToString(), "[img]" + match[0].Groups[1].Value + "[/img]", RegexOptions.IgnoreCase);

            return desc;
        }

        //
        // GET: /News/Edit/5
        
        public ActionResult Edit(int id = 0)
        {
            
            News news = db.News.Find(id);
            if (news == null)
            {
                return HttpNotFound();
            }
            //ViewBag.UserId = html.SelectListAdmin(news.UserId);
            ViewBag.CateId = html.SelectListCate(news.CateId, true); 

            return View(news);
        }

        //
        // POST: /News/Edit/5

        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(News news)
        {
            if (ModelState.IsValid)
            {

                 news.Image+="";
                 news.CateName = db.Category.Find(news.CateId).Name; 
                db.Entry(news).State = EntityState.Modified;
                db.SaveChanges();
                return Redirect("/News/Index?CateId=" + news.CateId);
            }
            return View(news);
        }

        //
        // GET: /News/Delete/5

        public ActionResult Delete(int id = 0)
        {
            News news = db.News.Find(id);
            if (news == null)
            {
                return HttpNotFound();
            }
            return View(news);
        }

        //
        // POST: /News/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            News news = db.News.Find(id);
            int CateId = news.CateId;
            db.News.Remove(news);
            db.SaveChanges();
            return Redirect("/News/Index?CateId=" + CateId);
        }
       
        
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}